package com.weijian.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijian.common.pojo.CommonResult;
import com.weijian.common.pojo.PageResponse;
import com.weijian.dal.dto.OrderDetailDTO;
import com.weijian.dal.entity.Item;
import com.weijian.dal.query.ItemPageQuery;
import com.weijian.dal.vo.ItemPageReqVO;
import com.weijian.service.ItemService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/items")
@RequiredArgsConstructor
@Slf4j
public class ItemController {

    private final ItemService itemService;

    @Operation(summary = "分页查询商品")
    @PostMapping("/list")
    public PageResponse<ItemPageReqVO> queryItemByPage(@RequestBody ItemPageQuery query) {
        Page<Item> itemPage = itemService.lambdaQuery()
                .like(StrUtil.isNotBlank(query.getKey()), Item::getName, query.getKey())
                .eq(StrUtil.isNotBlank(query.getBrand()), Item::getBrand, query.getBrand())
                .eq(StrUtil.isNotBlank(query.getCategory()), Item::getCategory, query.getCategory())
                .eq(Item::getStatus, 1)
                .between(query.getMaxPrice() != null, Item::getPrice, query.getMinPrice(), query.getMaxPrice())
                .page(query.toMpPage("update_time", false));
        return PageResponse.of(itemPage, ItemPageReqVO.class);
    }

    @Operation(summary = "根据id批量查询商品")
    @GetMapping("/gets")
    public CommonResult<List<Item>> queryItemByIds(@RequestParam("ids") List<Long> ids) {
        return itemService.queryItemByIds(ids);
    }

    @Operation(summary = "根据id查询商品")
    @GetMapping("/get")
    public CommonResult<Item> queryItemById(@RequestParam("id") Long id) {
        return itemService.queryItemById(id);
    }

    @Operation(summary = "新增商品")
    @PostMapping("/create")
    public void saveItem(@RequestBody Item item) {
        itemService.save(item);
    }

    @Operation(summary = "更新商品状态")
    @PutMapping("/status/{id}/{status}")
    public void updateItemStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateById(item);
    }

    @Operation(summary = "更新商品")
    @PutMapping("/update")
    public void updateItem(@RequestBody Item item) {
        // 不允许修改商品状态，所以强制设置为null，更新时，就会忽略该字段
        item.setStatus(null);
        // 更新
        itemService.updateItem(item);
    }

    @Operation(summary = "根据id删除商品")
    @DeleteMapping("/delete")
    public void deleteItemById(@RequestParam("id") Long id) {
        itemService.deleteItemById(id);
    }

    @Operation(summary = "批量扣减库存")
    @PutMapping("/stock/deduct")
    public void deductStock(@RequestBody List<OrderDetailDTO> items) {
        itemService.deductStock(items);
    }
}
